import * as vscode from "vscode";
import { triggerUpdateDecorations } from "./chinese-decoration";
import { TargetStr } from "./const";
import { showWebView } from "./show-web-view";
import {
  createTranslationFiles,
  getRelativePath,
  replaceTextInEditor,
} from "./extension-helper";
import { findChinese } from "./find-Chinese";

/**
 * 主入口文件
 * @param context
 */
export function activate(context: vscode.ExtensionContext) {
  console.log('Congratulations, your extension "my-extension" is now active!');

  let activeEditor = vscode.window.activeTextEditor;
  if (!activeEditor) {
    return;
  }

  context.subscriptions.push(
    // TODO5：完善翻译文本的命令
    vscode.commands.registerCommand("my-extension.translateText", (args) => {
      // const wordRange = args.range;
      // const fromWindowEditor = vscode.window.activeTextEditor;
      // const translateText = args.text;

      return new Promise(async (resolve) => {
        // const fileUrl = getRelativePath(activeEditor as vscode.TextEditor);
        // const res = await showWebView(translateText, fileUrl);

        // vscode.window.showTextDocument(
        //   fromWindowEditor?.document as vscode.TextDocument,
        //   {
        //     viewColumn: vscode.ViewColumn.One,
        //     preserveFocus: false,
        //     preview: false,
        //   }
        // );

        // return resolve(res);
      }).then(async (res: any) => {
        // 减1和加1是为了去除字符串双引号
        // const start = new vscode.Position(
        //   wordRange[0].line,
        //   wordRange[0].character - 1
        // );
        // const end = new vscode.Position(
        //   wordRange[1].line,
        //   wordRange[1].character + 1
        // );
        // const range = new vscode.Range(start, end);

        // replaceTextInEditor(
        //   fromWindowEditor as vscode.TextEditor,
        //   range,
        //   res.key
        // );

        // await createTranslationFiles(res.key, translateText, res.en);
      });
    })
  );

  // TODO2：获取当前文档的中文并添加红框装饰
  let targetStrs: TargetStr[] = [];
  const filePath = activeEditor.document.uri.fsPath;
  if (activeEditor && !filePath.includes("/i18n/")) {
    // triggerUpdateDecorations((newTargetStrs: TargetStr[]) => {
    //   targetStrs = newTargetStrs;
    // });
  }

  context.subscriptions.push(
    vscode.commands.registerCommand("my-extension.findChinese", findChinese)
  );

// TODO8：切换文档时重新检测当前文档中的中文文案
  context.subscriptions.push(
    vscode.window.onDidChangeActiveTextEditor((editor: any) => {
      // activeEditor = editor;
      // const filePath = editor.document.uri.fsPath;
      // if (editor && !filePath.includes("/i18n/")) {
      //   triggerUpdateDecorations((newTargetStrs: TargetStr[]) => {
      //     targetStrs = newTargetStrs;
      //   });
      // }
    }, null)
  );

  // TODO9：当文档内容发生变化时的时候重新检测当前文档中的中文文案
  context.subscriptions.push(
    vscode.workspace.onDidChangeTextDocument((event) => {
      // if (activeEditor && event.document === activeEditor.document) {
      //   triggerUpdateDecorations((newTargetStrs: TargetStr[]) => {
      //     targetStrs = newTargetStrs;
      //   });
      // }
    }, null)
  );
}

export function deactivate() {}
